﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATW.MT.BUS;
using System.Data;
using ATW.MT.CommonClass;
using System.Text;

namespace ATW.MT.BaseManage
{
    public partial class CountryManage : CommonPage
    {
        bus_D_Country Country = new bus_D_Country();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                InitList();
            }

        }
        private void BindData()
        {
            try
            {
                string name = txtName.Value.Trim();
                string strCode = txtCode.Value.Trim();
                string strAbbr = txtAbbr.Value.Trim();

                DataTable dt = Country.Country(name, strCode, strAbbr, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    AspNetPager1.RecordCount = Convert.ToInt32(dt.Rows[0]["totalpagesize"]);
                }
                else
                {
                    AspNetPager1.RecordCount = 0;
                }
                rpList.DataSource = dt;
                rpList.DataBind();
            }
            catch (Exception ex)
            {

            }
        }
        private void InitList()
        {
            try
            {
                DataTable dt = Country.getLanList();
                if (dt != null && dt.Rows.Count > 0)
                {
                    ddlLan.Items.Clear();
                    ddlLan.DataSource = dt;
                    ddlLan.DataBind();
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }
        protected void AspNetPager1_pageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        //新增
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(textCountry.Value))
                {
                    Response.Write("<script>alert('国家名称不能为空！');</script>");
                    return;
                }
                if (string.IsNullOrWhiteSpace(textCode.Value))
                {
                    Response.Write("<script>alert('国家区号不能为空！');</script>");
                    return;
                }
                if (string.IsNullOrWhiteSpace(textAbbr.Value))
                {
                    Response.Write("<script>alert('国家缩写不能为空！');</script>");
                    return;
                }
                string lanid = ddlLan.SelectedValue;
                string StrCountry = textCountry.Value;
                int intCode = Convert.ToInt32(textCode.Value);
                string strAbbr = textAbbr.Value;
                if (Country.isCountryExist(StrCountry))
                {
                    Tips("该国家已经存在！");
                    return;
                }
                int result = Country.AddItem(StrCountry, intCode, strAbbr,lanid);
                if (result > 0)
                {
                    Tips("新增成功");
                    BindData();
                }
                else
                {
                    Tips("新增失败！");
                    return;
                }
            }
            catch (Exception ex)
            {
                Tips(ex.Message);
            }
        }
        //删除
        protected void lbnDelete_Click(object sender, EventArgs e)
        {

            LinkButton lbn = (LinkButton)sender;
            string strId = lbn.CommandArgument;
            try
            {
                int result = Country.CountryChangeStatus(strId, "2");
                if (result > 0)
                {
                    //ShowMsgTips("删除成功", 2);
                    Tips("删除成功");
                    BindData();
                }
                else
                {
                    //ShowMsgTips("删除失败", 2);
                    Tips("删除失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        private void Tips(string s)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }//提示
    }
}